nohup python main.py > monitor.log 2>&1 &

#或
screen -S monitor
python main.py
# 按 Ctrl+A D 可断开会话，后台继续运行

#或 注册为 systemd 服务（推荐生产环境）  推荐

 sudo  vim /etc/systemd/system/monitor.service

#编辑

[Unit]
Description=Python Monitor Service
After=network.target

[Service]
Type=simple
User=youruser
WorkingDirectory=/home/youruser/monitor
ExecStart=/usr/bin/python3 main.py
Restart=always
RestartSec=5
StandardOutput=append:/home/youruser/monitor/monitor.log
StandardError=append:/home/youruser/monitor/monitor.err

[Install]
WantedBy=multi-user.target

# 服务状态
sudo systemctl daemon-reload
sudo systemctl enable monitor
sudo systemctl start monitor

